/*
 * windows_serial_port.h
 *
 *  Created on: 2010-3-26
 *      Author: Administrator
 */

#ifndef WINDOWS_SERIAL_PORT_H_
#define WINDOWS_SERIAL_PORT_H_

#include <windows.h>

class WindowsSerialPort {
public:
	WindowsSerialPort();
	~WindowsSerialPort();
	/*
	 * exist: Test serial port is existing
	 * Parameters:
	 *   portName: an array of chars containing a NULL terminated string
	 * Returns: a boolean indicating existing serial port
	 */
	static bool exist(char* portName);
	/*
	 * open: opens the serial port using port-name provided.
	 * Parameters:
	 *   portName: an array of chars containing a NULL terminated string
	 *     that is the port name that we want to open.
	 *     eg. COM1, COM2, COM4
	 * Returns: a boolean indicating success or failure
	 */
	bool open(char * portName);

	/*
	 * close: closes the serial port
	 * Parameters: None.
	 * Returns: a boolean indicating success or failure
	 */
	bool close();

	/*
	 * setSettings: Set the settings of the serial port interface. Should
	 *   be called after the port is opened because otherwise settings
	 *   could be lost.
	 * Parameters:
	 *   speed: the desired speed of the serial port
	 *   wordSize: the size of the data word in bits
	 *   stopBits: the number of stop bits
	 *   parity: parity to be used. One of
	 *     'n' = None
	 *     'e' = Even
	 *     'o' = Odd
	 * Returns: boolean indicating success or failure
	 */
	bool setSettings(int speed, int wordSize, int stopBits, char parity);
	/*
	 * setTimeouts: set the read and write timeouts of the serial port
	 * Parameters:
	 *   readTimeout: The amount of time to wait for a single word read to timeout
	 *   writeTimeout: The amount of time to wait for a single word write to timeout
	 * Returns: boolean indicating success or failure
	 */
	bool setTimeouts(int readTimeout, int writeTimeout);
	/*
	 * readBuf: read in a buffer of words
	 * Parameters:
	 *   buf: The buffer to read words into
	 *   bufSize: the number of words to read. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually read
	 */
	int readBuf(char * buf, int bufSize);

	/*
	 * writeBuf: write out a buffer of words
	 * Parameters:
	 *   buf: The buffer to write words from
	 *   bufSize: the number of words to write. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually written
	 */
	int writeBuf(char * buf, int bufSize);
private:
	HANDLE fileHandle;
	int readTimeout;
	int writeTimeout;

};


#endif /* WINDOWS_SERIAL_PORT_H_ */
